package Offer.offer2020;

import java.util.Stack;

/**
 * 栈的压入，弹出序列
 *
 * @Author: leiwenkang02
 * @Date: 2020/3/27 11:29 上午
 */
public class Test22 {
    public static void main(String[] args) {
        int[] pushArray = {1, 2, 3, 4, 5};
        int[] popArray = {1, 2, 3, 4, 5};
        System.out.println(isPopOrder(pushArray, popArray));
    }


    public static boolean isPopOrder(int[] pushOrder, int[] popOrder) {
        if (pushOrder == null || pushOrder.length == 0
                || popOrder == null || popOrder.length == 0
                || pushOrder.length != popOrder.length) {
            return false;
        }

        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushOrder.length; i++) {
            stack.push(pushOrder[i]);
            while (!stack.isEmpty() && j < popOrder.length && popOrder[j] == stack.peek()) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();

    }
}
